<template>
  <div class="layout-padding">
    <div class="layout-padding-auto layout-padding-view">
      <el-row v-show="showSearch">
        <el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
          <el-form-item label="抵押人名称" prop="dyrMc">
            <el-input placeholder="请输入抵押人名称" v-model="state.queryForm.dyrMc"
                      style="max-width: 180px"/>
          </el-form-item>
          <el-form-item label="抵押人代理人" prop="dyrDlr">
            <el-input placeholder="请输入抵押人代理人" v-model="state.queryForm.dyrDlr"
                      style="max-width: 180px"/>
          </el-form-item>
          <el-form-item label="抵押权人名称" prop="dyqrMc">
            <el-input placeholder="请输入抵押权人名称" v-model="state.queryForm.dyqrMc"
                      style="max-width: 180px"/>
          </el-form-item>
          <el-form-item label="抵押权人代理人" prop="dyqrDlr">
            <el-input placeholder="请输入抵押权人代理人" v-model="state.queryForm.dyqrDlr"
                      style="max-width: 180px"/>
          </el-form-item>
          <el-form-item>
            <el-button icon="search" type="primary" @click="getDataList">
              查询
            </el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <el-row>
        <div class="mb8" style="width: 100%">
          <el-button icon="folder-add" type="primary" class="ml10" @click="formDialogRef.openDialog()"
                     v-auth="'admin_mortgageApplyRegistration_add'">
            新 增
          </el-button>
          <el-button plain :disabled="multiple" icon="Delete" type="primary"
                     v-auth="'admin_mortgageApplyRegistration_del'" @click="handleDelete(selectObjs)">
            删除
          </el-button>
          <right-toolbar v-model:showSearch="showSearch" :export="'admin_mortgageApplyRegistration_export'"
                         @exportExcel="exportExcel" class="ml10 mr20" style="float: right;"
                         @queryTable="getDataList"></right-toolbar>
        </div>
      </el-row>
      <el-table :data="state.dataList" v-loading="state.loading" border
                :cell-style="tableStyle.cellStyle" :header-cell-style="tableStyle.headerCellStyle"
                @selection-change="selectionChangeHandle" @sort-change="sortChangeHandle">
        <el-table-column type="selection" width="40" align="center"/>
        <el-table-column type="index" label="#" width="40"/>
        <el-table-column label="抵押人">
          <el-table-column prop="dyrMc" label="名称" show-overflow-tooltip  width="100"/>
          <el-table-column prop="dyrDz" label="地址" show-overflow-tooltip width="100"/>
          <el-table-column prop="dyrFrdb" label="法人代表" show-overflow-tooltip width="100"/>
          <el-table-column prop="dyrDlr" label="代理人" show-overflow-tooltip width="100"/>
        </el-table-column>
        <el-table-column label="抵押权人">
          <el-table-column prop="dyqrMc" label="名称" show-overflow-tooltip width="100"/>
          <el-table-column prop="dyqrDz" label="地址" show-overflow-tooltip width="100"/>
          <el-table-column prop="dyqrFrdb" label="法人代表" show-overflow-tooltip width="100"/>
          <el-table-column prop="dyqrDlr" label="代理人" show-overflow-tooltip width="100"/>
        </el-table-column>
        <el-table-column prop="djyy" label="申请抵押物登记的原因" show-overflow-tooltip width="150"/>
        <el-table-column prop="zzqzl" label="被担保的主债权种类" show-overflow-toolti width="150"/>
        <el-table-column prop="zzqse" label="被担保的主债权数额" show-overflow-tooltip width="150"/>
        <el-table-column prop="lxzwqx" label="债务人履行债务的期限" show-overflow-tooltip width="150"/>
        <el-table-column prop="dydbfw" label="抵押担保范围" show-overflow-tooltip width="150"/>
        <el-table-column prop="dyqr" label="抵押起日" show-overflow-tooltip width="100"/>
        <el-table-column prop="dyzr" label="抵押止日" show-overflow-tooltip width="100"/>
        <el-table-column prop="shrrq" label="审核日期" show-overflow-tooltip width="100"/>
        <el-table-column prop="jbrq" label="经办日期" show-overflow-tooltip width="100"/>
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button text type="primary" icon="view" v-auth="'sys_role_edit'"
                       @click="formDialogRef.openDialog(scope.row.id, true)">
              详情
            </el-button>
            <el-button icon="edit-pen" text type="primary" v-auth="'admin_mortgageApplyRegistration_edit'"
                       @click="formDialogRef.openDialog(scope.row.id)">编辑
            </el-button>
            <el-button icon="delete" text type="primary" v-auth="'admin_mortgageApplyRegistration_del'"
                       @click="handleDelete([scope.row.id])">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination"/>
    </div>

    <!-- 编辑、新增  -->
    <form-dialog ref="formDialogRef" @refresh="getDataList(false)"/>
  </div>
</template>

<script setup lang="ts" name="systemMortgageApplyRegistration">
import {BasicTableProps, useTable} from "/@/hooks/table";
import {fetchList, delObjs} from "/@/api/forestry/mortgageApplyRegistration";
import {useMessage, useMessageBox} from "/@/hooks/message";
import {useDict} from '/@/hooks/dict';
// 引入组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));

// 定义查询字典

// 定义变量内容
const formDialogRef = ref()
// 搜索变量
const queryRef = ref()
const showSearch = ref(true)
// 多选变量
const selectObjs = ref([]) as any
const multiple = ref(true)

const state: BasicTableProps = reactive<BasicTableProps>({
  queryForm: {},
  pageList: fetchList
})

//  table hook
const {
  getDataList,
  currentChangeHandle,
  sizeChangeHandle,
  sortChangeHandle,
  downBlobFile,
  tableStyle
} = useTable(state)

// 清空搜索条件
const resetQuery = () => {
  // 清空搜索条件
  queryRef.value?.resetFields()
  // 清空多选
  selectObjs.value = []
  getDataList()
}

// 导出excel
const exportExcel = () => {
  downBlobFile('/admin/mortgageApplyRegistration/export', Object.assign(state.queryForm, {ids: selectObjs}), 'mortgageApplyRegistration.xlsx')
}

// 多选事件
const selectionChangeHandle = (objs: { id: string }[]) => {
  selectObjs.value = objs.map(({id}) => id);
  multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
  try {
    await useMessageBox().confirm('此操作将永久删除');
  } catch {
    return;
  }

  try {
    await delObjs(ids);
    getDataList();
    useMessage().success('删除成功');
  } catch (err: any) {
    useMessage().error(err.msg);
  }
};
</script>
